package com.pgytesting.secondhandcar.service.impl;

import com.pgytesting.secondhandcar.model.Power;
import com.pgytesting.secondhandcar.model.RolePowerRelation;
import com.pgytesting.secondhandcar.dao.PowerDao;
import com.pgytesting.secondhandcar.dao.RolePowerRelationDao;
import com.pgytesting.secondhandcar.service.PowerService;
import com.pgytesting.secondhandcar.util.SessionUtil;
import com.pgytesting.secondhandcar.util.UtilMethod;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Power)表服务实现类
 *
 * @author makejava
 * @since 2019-08-16 15:20:21
 */
@Service("powerService")
public class PowerServiceImpl implements PowerService {
    @Resource
    private PowerDao powerDao;
    @Autowired
    private RolePowerRelationDao rolePowerRelationDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Power queryById(Integer id) {
        return this.powerDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public List<Power> queryAllByLimit(int offset, int limit) {
        return this.powerDao.queryAllByLimit(offset, limit);
    }
    
    /**
     * 条件查询多条数据
     *
     * @param power 实例对象
     * @return 对象列表
     */
    @Override
    public List<Power> queryAll(Power power) {
        return this.powerDao.queryAll(power);
    }

    /**
     * 新增数据
     *
     * @param power 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public Power insert(Power power) {
    	power.setStatus(0);
    	power.setCreatedBy(SessionUtil.getSessionAdmin().getId().toString());
    	power.setCreatedTime(UtilMethod.unixLong());
        this.powerDao.insert(power);
        return power;
    }

    /**
     * 修改数据
     *
     * @param power 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public Power update(Power power) {
    	power.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
        this.powerDao.update(power);
        return this.queryById(power.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
    public boolean deleteById(Integer id) {
        return this.powerDao.deleteById(id) > 0;
    }

	@Override
	public Power queryObj(Power power) {
		power.setStatus(0);
		return powerDao.queryObj(power);
	}

	@Override
	@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED, rollbackFor = Exception.class)
	public void deleteObj(Integer id) {
		Power power = new Power();
		power.setId(id);
		power.setStatus(-1);
		power.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
		powerDao.update(power);
		
		RolePowerRelation rolePowerRelation = new RolePowerRelation();
		rolePowerRelation.setPowerId(id);
		rolePowerRelation.setStatus(-1);
		rolePowerRelation.setUpdatedBy(SessionUtil.getSessionAdmin().getId().toString());
		rolePowerRelationDao.update(rolePowerRelation);
	}
}